//
//  WKNetWorkTool.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/26.
//  Copyright © 2016年 恋guang年. All rights reserved.
//
/// 网络工具单列类
import UIKit
import AFNetworking
class WKNetWorkTool: NSObject {
    static let sharedInstance : WKNetWorkTool = WKNetWorkTool()
    private let  afnNetWorkTool : AFHTTPSessionManager = AFHTTPSessionManager(baseURL: WKBaseURL)
    private override init(){
        super.init()
        afnNetWorkTool.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    
    //封装GET请求
    func requestGET(URLString:String, parameters:AnyObject?,requestCallBack:(result:[String:AnyObject]?,error:NSError?) ->()){
        afnNetWorkTool.GET(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            //执行闭包 完成回调
            //返回的基本都是JSON 转成字典
            let dict = result as? [String : AnyObject]
            requestCallBack(result: dict, error: nil)
            }) { (_, error) -> Void in
                //执行闭包 失败回调
                requestCallBack(result:nil, error: error)
        }
        
    }
    
    //封装POST请求
    func requestPOST(URLString:String, parameters:AnyObject?,requestCallBack:(result:[String:AnyObject]?,error:NSError?) ->()){
        afnNetWorkTool.POST(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            //执行闭包 完成回调
            //返回的基本都是JSON 转成字典
            let dict = result as? [String : AnyObject]
            requestCallBack(result: dict, error: nil)
            requestCallBack(result: dict, error: nil)
            }) { (_, error) -> Void in
                //执行闭包 失败回调
                requestCallBack(result:nil, error: error)
        }
        
    }
    
    
}
